package com;

public class LeetCode {
    public int lengthOfLongestSubstring(String s) {

        // 记录字符上一次出现的位置
        int[] last = new int[50000];
        int n = s.length();
        for (int i = 0; i <last.length ; i++) {
            last[i]=-1;
        }
        int res = 0;
        int start = 0; // 窗口开始位置
        int end=0;
        for(int i = 0; i < n; i++) {
            int index = s.charAt(i);
            start = Math.max(start, last[index]+1);
            int temp = res;
            res   = Math.max(res, i - start+1);
            if(temp!=res){
                end=i+1;
            }
            last[index] = i;
        }

        System.out.println(s.substring(end-res,end));
        return res;
    }

    public static void main(String[] args) {
        LeetCode leetCode = new LeetCode();
        System.out.println(leetCode.lengthOfLongestSubstring("pwwkew"));

    }
}
